package main.java.apacha;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @Auther:Liu
 * @Testname:EmpDaolmpl
 * @Date:2025/3/19 9:37
 */
public class EmpDaolmpl {
    private QueryRunner queryRunner = new QueryRunner();

    public static void main(String[] args) throws SQLException {
        EmpDaolmpl empDaolmpl = new EmpDaolmpl();

        List<manage> manages = empDaolmpl.selectAll();
        for (int i = 0; i <manages.size() ; i++) {
            System.out.println(manages.get(i).getId()+"   "+manages.get(i).getAdmire()+"   "+manages.get(i).getPassword());
        }
//        System.out.println(manages);
//
//        manage select = empDaolmpl.select(2);
//        System.out.println(select);

//        int delete = empDaolmpl.delete(15);
//        System.out.println(delete);

//        manage man = new manage("郭芙蓉", "114514");
//        int insert = empDaolmpl.insert(man);
//        System.out.println(insert);
    }

    /*
     * 1.查询所有的数据
     * */
//    @Override
    public List<manage> selectAll() throws SQLException {


            List<manage> emps = queryRunner.query(DbUtils.getConnection(),
                    "select* from manage;",
                    new BeanListHandler<manage>(manage.class));
            return emps;



    }

    /*
     * 2.实现删除的业务逻辑接口
     * */
    public int delete(int id) throws SQLException {

            int result = queryRunner.update(DbUtils.getConnection(),
                    "delete from manage where id=?;", id);
            return result;


    }

    /*
     * 3.更新的业务逻辑
     * */
//    @Override
    public int update(manage emp) {
        try {
            int result = queryRunner.update(DbUtils.getConnection(),
                    "update emp set admire=?,password=? where id=?;",
                    emp.getAdmire(), emp.getPassword(), emp.getId());

            return result;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    /*
     * 选择一条数据
     * */
//    @Override
    public manage select(int id) {
        System.out.println("数据操作层：id = " + id);
        try {

            manage emp = queryRunner.query(DbUtils.getConnection(),
                    "select* from manage where id=?;",
                    new BeanHandler<manage>(manage.class), id);

            return emp;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    /*
     * 插入一条数据
     * */
//    @Override
    public int insert(manage emp) {
        System.out.println("数据操作的DAO：emp.toString() = " + emp.toString());
        try {
//            int result =queryRunner.update(DbUtils.getConnection(),
//                    "update empset name=?,salary=?,age=? where id=?",
//                    emp.getName(),emp.getSalary(), emp.getAge(), emp.getId());
            int result = queryRunner.
                    update(DbUtils.getConnection(),
                    "insert into manage(admire,password) values(?,?);",
                    emp.getAdmire(), emp.getPassword());
            System.out.println("数据入库成功啦--result =" + result);
            return result;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }
}
